<html>
<head><meta charset="utf-8"><title>Lifetime error only with LateContext · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html">Lifetime error only with LateContext</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239590644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239590644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239590644">(May 20 2021 at 13:26)</a>:</h4>
<p>I put the following code in rustc_lint/src/builtin.rs and I get a lifetime error on the last line. I can't reproduce the error with anything other than <code>LateContext</code>. What am I missing?</p>
<p>Also, I first ran into this in clippy, but there the error looked different and was less helpful: "these two types are declared with different lifetimes..." but it's not about two types.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">bok_ref</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="nc">std</span>::<span class="n">cell</span>::<span class="n">Ref</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">bok_cx</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="nc">LateContext</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">test</span><span class="p">(</span><span class="n">rf</span>: <span class="kp">&amp;</span><span class="nc">std</span>::<span class="n">cell</span>::<span class="n">Ref</span><span class="o">&lt;'</span><span class="nb">_</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">cx</span>: <span class="kp">&amp;</span><span class="nc">LateContext</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">bok_ref</span><span class="p">(</span><span class="n">rf</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">bok_cx</span><span class="p">(</span><span class="n">cx</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="239591588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239591588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239591588">(May 20 2021 at 13:33)</a>:</h4>
<p><span class="user-mention" data-user-id="360405">@Cameron Steffen</span> Can you paste the error you're getting?</p>



<a name="239591729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239591729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239591729">(May 20 2021 at 13:33)</a>:</h4>
<p>in rustc</p>
<div class="codehilite" data-code-language="Text only"><pre><span></span><code>error: lifetime may not live long enough
   --&gt; compiler/rustc_lint/src/builtin.rs:167:5
    |
164 | pub fn test(x: &amp;X&lt;'_&gt;, rf: &amp;std::cell::Ref&lt;'_, u32&gt;, cx: &amp;LateContext&lt;'_&gt;) {
    |                                                          -            -- let's call this `'2`
    |                                                          |
    |                                                          let's call the lifetime of this reference `'1`
...
167 |     bok_cx(cx);
    |     ^^^^^^^^^^ argument requires that `'1` must outlive `'2`
</code></pre></div>
<p>in clippy</p>
<div class="codehilite" data-code-language="Text only"><pre><span></span><code>error[E0623]: lifetime mismatch
  --&gt; src/tools/clippy/clippy_utils/src/hir_utils.rs:31:12
   |
28 | pub fn test(x: &amp;X&lt;'_&gt;, rf: &amp;std::cell::Ref&lt;'_, u32&gt;, cx: &amp;LateContext&lt;'_&gt;) {
   |                                                          ----------------
   |                                                          |
   |                                                          these two types are declared with different lifetimes...
...
31 |     bok_cx(cx);
   |            ^^ ...but data from `cx` flows into `cx` here

error: aborting due to previous error
</code></pre></div>



<a name="239591839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239591839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239591839">(May 20 2021 at 13:34)</a>:</h4>
<p>here's a reproduction: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=3264cce0c703449fdb25e42b5aaadd55">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=3264cce0c703449fdb25e42b5aaadd55</a></p>



<a name="239591887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239591887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239591887">(May 20 2021 at 13:34)</a>:</h4>
<p>the issue is that <code>LateContext</code> is invariant over its lifetime</p>



<a name="239591906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239591906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239591906">(May 20 2021 at 13:34)</a>:</h4>
<p><code>LateContext</code>contains a <code>Cell&lt;Option&lt;&amp;'tcx TypeckResults&lt;'tcx&gt;&gt;&gt;</code> which makes it invariant over its lifetime</p>



<a name="239592010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239592010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239592010">(May 20 2021 at 13:35)</a>:</h4>
<p>I'm working on <a href="https://github.com/rust-lang/rust/pull/85343">https://github.com/rust-lang/rust/pull/85343</a>, which will improve the error message</p>



<a name="239592259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239592259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239592259">(May 20 2021 at 13:37)</a>:</h4>
<p>thank you! I'll have to read the nomicon to understand...</p>



<a name="239592294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239592294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239592294">(May 20 2021 at 13:37)</a>:</h4>
<p>any idea why the error differs?</p>



<a name="239592748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239592748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239592748">(May 20 2021 at 13:40)</a>:</h4>
<p>The error in rustc occurs because the crate gets built with <code>#![feature(nll)]</code> or <code>-Z borrowck=mir</code></p>



<a name="239592776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239592776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239592776">(May 20 2021 at 13:40)</a>:</h4>
<p>for whatever reason, that's not happening with clippy</p>



<a name="239592875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239592875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239592875">(May 20 2021 at 13:41)</a>:</h4>
<p>So the better error message is basically an unstable feature?</p>



<a name="239593249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239593249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239593249">(May 20 2021 at 13:43)</a>:</h4>
<p>sort of - <code>#![feature(nll)]</code> performs 'more' borrow-checking on the MIR. That enables us to accept more code, and also produces nicer errors in some cases</p>



<a name="239593607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Lifetime%20error%20only%20with%20LateContext/near/239593607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Lifetime.20error.20only.20with.20LateContext.html#239593607">(May 20 2021 at 13:45)</a>:</h4>
<p>Sure enough. I got the better error in clippy. Should we add <code>#![feature(nll)]</code>?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>